//
//  YFMyGiftsVC.m
//  project
//
//  Created by yefei on 2019/4/15.
//  Copyright © 2019 健健. All rights reserved.
//

#import "YFMyGiftsVC.h"
#import "TYTabPagerView.h"
#import "TYTabPagerController.h"
#import "YFMyRecivedGiftsVC.h"
#import "YFMySendedGiftsVC.h"

@interface YFMyGiftsVC () <TYTabPagerBarDataSource ,TYTabPagerBarDelegate, TYPagerControllerDataSource, TYPagerControllerDelegate>
/**
 分页TabBar
 */
@property (nonatomic, strong, nonnull) TYTabPagerBar *tabBar;
/**
 分页控制器
 */
@property (nonatomic, strong, nonnull) TYPagerController *pagerController;
/**
 标题数组
 */
@property (nonatomic, copy, nonnull) NSArray<NSString *> *titles;
/**
 控制器
 */
@property (nonatomic, strong, nonnull) NSArray<UIViewController *> *controllers;

@property (strong, nonatomic) UIView *contentView;

@property (nonatomic, strong) UIButton *selectedAllButton;



@end

@implementation YFMyGiftsVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navTitle = @"我的礼物";
    [self setupViews];
    [self loadData];

}

- (void)viewWillLayoutSubviews {
    [super viewWillLayoutSubviews];
    [self setupFrames];
    
}

#pragma mark - 设置UI
- (void)setupViews {
    [self setupNavigationRightButton];
    [self setupContentView];
    [self setupTitlesTabBar];
    [self setupPagesController];
}

- (void)setupNavigationRightButton {
    UIButton *button = [UIButton buttonWithType:(UIButtonTypeCustom)];
    button.frame = CGRectMake(0, 0, 60, 44);
    button.titleLabel.font = [UIFont fontWithName:PingFangSC_Medium size:16];
    [button setTitleColor:[UIColor colorWithHex:0x666666] forState:(UIControlStateNormal)];
    [button setTitleColor:[UIColor lightGrayColor] forState:(UIControlStateHighlighted)];
    [button setTitle:@"全选" forState:(UIControlStateNormal)];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:button];
    [[button rac_signalForControlEvents:(UIControlEventTouchUpInside)] subscribeNext:^(__kindof UIControl * _Nullable x) {
        x.selected = !x.selected;
        [button setTitle:(x.selected ? @"取消" : @"全选") forState:(UIControlStateNormal)];
        NSDictionary *info = @{@"isSelected": @(x.selected)};
        [[NSNotificationCenter defaultCenter] postNotificationName:NotificationSelectedAllGifts object:nil userInfo:info];
    }];
    self.selectedAllButton = button;
}

- (void)setupContentView {
    CGFloat height = SCREEN_HEIGTH - NAVIGATION_BAR_HEIGHT - 5 - (IS_FULL_SCREEN ? SAFE_AREA_HEIGHT : 12);
    UIView *contentView = [[UIView alloc] initWithFrame:CGRectMake(12, NAVIGATION_BAR_HEIGHT + 5, SCREEN_WIDTH - 24, height)];
    contentView.backgroundColor = [UIColor whiteColor];
    [contentView rounded:(UIRectCornerAllCorners) radius:10];
    [self.view addSubview:contentView];
    self.contentView = contentView;
    
    UIView *line = [[UIView alloc] initWithFrame:CGRectMake(12, 40, height - 24, 1)];
    line.backgroundColor = [UIColor withRGB:242];
    [self.contentView addSubview:line];
}

#pragma mark - 加载数据
- (void)loadData {
    [self.tabBar reloadData];
    [self.pagerController reloadData];
}
#pragma mark - 设置 Frame
- (void)setupFrames {
    self.tabBar.frame = CGRectMake(0, 0, SCREEN_WIDTH - 24 , 40);
    CGFloat height = self.contentView.height - 42;
    self.pagerController.view.frame = CGRectMake(0, 41, SCREEN_WIDTH - 24, height);
}

#pragma mark - 设置上方标题 TabBar
- (void)setupTitlesTabBar {
    TYTabPagerBar *tabBar = [[TYTabPagerBar alloc] init];
    tabBar.layout.barStyle = TYPagerBarStyleProgressElasticView;
    tabBar.layout.normalTextFont = [UIFont fontWithName:PingFangSC_Medium size:16];
    tabBar.layout.selectedTextFont = [UIFont fontWithName:PingFangSC_Medium size:16];
    tabBar.layout.normalTextColor = [UIColor withRGB:51];
    tabBar.layout.selectedTextColor = [UIColor withRed:254 green:117 blue:127];
    tabBar.layout.progressColor = [UIColor withRed:254 green:117 blue:127];
    tabBar.layout.progressWidth = [self heightWithTitle];
    tabBar.layout.progressHeight = 1;
    tabBar.layout.cellWidth = (SCREEN_WIDTH - 40 - 24) / self.titles.count;
    tabBar.layout.adjustContentCellsCenter = YES;
    tabBar.layout.cellEdging = 0;
    tabBar.layout.cellSpacing = 0;
    tabBar.dataSource = self;
    tabBar.delegate = self;
    [tabBar registerClass:[TYTabPagerBarCell class] forCellWithReuseIdentifier:[TYTabPagerBarCell cellIdentifier]];
    [self.contentView addSubview:tabBar];
    self.tabBar = tabBar;
}

#pragma mark - 设置PagerController
- (void)setupPagesController {
    TYPagerController *pagerController = [[TYPagerController alloc] init];
    pagerController.layout.prefetchItemCount = 1;
    //pagerController.layout.autoMemoryCache = NO;
    // 只有当scroll滚动动画停止时才加载pagerview，用于优化滚动时性能
    pagerController.layout.addVisibleItemOnlyWhenScrollAnimatedEnd = YES;
    pagerController.dataSource = self;
    pagerController.delegate = self;
    [self addChildViewController:pagerController];
    [self.contentView addSubview:pagerController.view];
    _pagerController = pagerController;
}

#pragma mark - TYTabPagerBarDataSource ,TYTabPagerBarDelegate
- (NSInteger)numberOfItemsInPagerTabBar {
    return self.titles.count;
}

- (UICollectionViewCell<TYTabPagerBarCellProtocol> *)pagerTabBar:(TYTabPagerBar *)pagerTabBar
                                              cellForItemAtIndex:(NSInteger)index {
    UICollectionViewCell<TYTabPagerBarCellProtocol> *cell = [pagerTabBar dequeueReusableCellWithReuseIdentifier:[TYTabPagerBarCell cellIdentifier] forIndex:index];
    cell.titleLabel.text = self.titles[index];
    return cell;
}

- (CGFloat)pagerTabBar:(TYTabPagerBar *)pagerTabBar widthForItemAtIndex:(NSInteger)index {
    NSString *title = self.titles[index];
    return [pagerTabBar cellWidthForTitle:title];
}

- (void)pagerTabBar:(TYTabPagerBar *)pagerTabBar didSelectItemAtIndex:(NSInteger)index {
    [self.pagerController scrollToControllerAtIndex:index animate:YES];
}

#pragma mark - TYTabPagerControllerDataSource, TYTabPagerControllerDelegate
- (NSInteger)numberOfControllersInPagerController {
    return self.controllers.count;
}

- (UIViewController *)pagerController:(TYPagerController *)pagerController controllerForIndex:(NSInteger)index prefetching:(BOOL)prefetching {
    UIViewController *vc = self.controllers[index];
    
    return vc;
}

-(void)pagerController:(TYPagerController *)pagerController transitionFromIndex:(NSInteger)fromIndex toIndex:(NSInteger)toIndex animated:(BOOL)animated {
    [self.tabBar scrollToItemFromIndex:fromIndex toIndex:toIndex animate:animated];
    if (toIndex == 1) {
        self.selectedAllButton.hidden = YES;
    } else {
        self.selectedAllButton.hidden = NO;
    }
}

- (void)pagerController:(TYPagerController *)pagerController transitionFromIndex:(NSInteger)fromIndex toIndex:(NSInteger)toIndex progress:(CGFloat)progress {
    [self.tabBar scrollToItemFromIndex:fromIndex toIndex:toIndex progress:progress];
}

#pragma mark - Lazy Load
- (NSArray<NSString *> *)titles {
    return @[@"收到的礼物", @"送出的礼物"];
}

- (NSArray<UIViewController *> *)controllers {
    return @[[YFMyRecivedGiftsVC new], [YFMySendedGiftsVC new]];
}


#pragma mark - 计算 线宽
- (CGFloat)heightWithTitle {
    CGSize size = [@"收到的礼物" sizeWithFont:[UIFont fontWithName:PingFangSC_Medium size:16] boundingSize:CGSizeMake(CGFLOAT_MAX, 40)];
    return size.width;
}

@end
